<?php

namespace App\Http\Controllers\Admin;

use App\Models\Image;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

/**
 * @group 后台接口文档
 * @title 图片管理模块接口
 * @author chenke
 * @description 酒庄后台图片板块接口
 * @header name:X-Token type:string require:1 desc:登录令牌
 *  */
class Img extends Base
{
    /**
     * @title 图片列表
     * @description 后台图片数据列表
     * @url /admin/img/imgshow
     * @method GET
     * @module 图片管理板块
     * @author chenke
     * @param name:limit type:int require:1 default:1 other: desc:每页数据条数
     * @param name:page type:int require:1 default:1 other: desc:当前页数
     * @param name:searchcontent type:string require:0 default: other: desc:输入框搜索内容(对应图片英文名)
     * @param name:searchtype type:int require:0 default:0 other: desc:搜索图片类型(0为全部,1为商品图片,2为积分商品图片,3为广告图片,4为用户头像)
     * @param name:rid type:int require:0 default:0 other: desc:图片关联ID
     * @param name:startDate type:date require:0 default: other: desc:搜索日期（开始日期）
     * @param name:endDate type:date require:0 default: other: desc:搜索日期（结束日期）
     * @return code:错误码
     * @return message:错误提示
     * @return data:图片信息@
     * @data id:图片ID title:图片英文名 title_zn:图片中文名 src:图片地址 type:图片类型(同参数) rid:图片关联ID created_at:图片添加时间
     */
    public function imgList(Request $request){
        $data = $request->all();
        $limit = $data['limit'];
        $page = $data['page'];
        $searchCont = (isset($data['searchcontent']) && $data['searchcontent'])?$data['searchcontent']:'';
        $startDate = (isset($data['startDate']) && $data['startDate'])?$data['startDate']:'';
        $endDate = (isset($data['endDate']) && $data['endDate'])?$data['endDate']:'';
        $searchtype = (isset($data['searchtype']) && $data['searchtype'])?$data['searchtype']:0;
        $rid = (isset($data['rid']) && $data['rid'])?$data['rid']:0;
        $where = [];
        if($searchCont){
            $where[] = ['title','like','%'.$searchCont.'%'];
        }
        if($searchtype){
            $where[] = ['type','=',$searchtype];
        }
        if($rid){
            $where[] = ['rid','=',$rid];
        }
        $whereDates = [];
        if($endDate){
            $entime = strtotime($endDate) + 24*3600;
            $endDate = date('Y-m-d',$entime);
        }
        if($startDate && $endDate){
            $whereDates = [$startDate,$endDate];
        }
        $return = (new Image())->getPageList(['limit'=>$limit,'page'=>$page],$where,$whereDates);
        return response()->json($return);
    }

    /**
     * @title 图片编辑
     * @description 编辑单个图片信息
     * @url /admin/img/edtimg
     * @method POST
     * @module 图片管理板块
     * @author chenke
     * @param name:id type:int require:1 default:0 other: desc:用户ID
     * @param name:src type:string require:1 default: other: desc:商品主图
     * @param name:title type:string require:1 default: other: desc:商品英文名
     * @param name:title_zn type:string require:1 default: other: desc:商品中文名
     * @return code:错误码
     * @return message:错误提示
     * @return data:商品ID
     */
    public function imgUpdate(Request $request){
        $data = $request->post();
        $rules = [
            'id'=>'required|integer',
            'title'=>'required|string',
            'title_zn'=>'required|string',
            'src'=>'required|string'
        ];
        $messages = [
            'id.required'=> '请选择商品',
            'id.integer'=> '商品ID错误',
            'title.required'=> '请填写英文标题',
            'title_zn.required'=> '请填写中文标题',
            'src.required'=> '请上传图片'
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $result =  (new Image())->doUpdate($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'prodUpdate');
        }
        return response()->json($result);
    }

    /**
     * @title 删除图片
     * @description 删除图片
     * @url /admin/img/delimg
     * @method POST
     * @module 图片管理板块
     * @author chenke
     * @param name:id type:int require:1 default:0 other: desc:用户ID
     * @return code:错误码
     * @return message:错误提示
     * @return data:正确时为空
     */
    public function deleteImg(Request $request){
        $id = $request->post('id');
        try{
            $result = (new Image())->where('id',$id)->delete();
            return response()->json([
                'code'=>0,
                'data'=>''
            ]);
        }catch (\Exception $e){
            $this->logErr($e->getCode(),$e->getMessage(),'deleteImg');
            return response()->json([
                'code'=>1,
                'message'=>'删除失败'
            ]);
        }
    }

    /**
     * @title 轮播图列表
     * @description 后台轮播图
     * @url /admin/img/lunboshow
     * @method GET
     * @module 轮播图板块
     * @author chenke
     * @param name:limit type:int require:1 default:1 other: desc:每页数据条数
     * @param name:page type:int require:1 default:1 other: desc:当前页数
     * @param name:type type:int require:1 default:3 other: desc:轮播图类别(3首页轮播,5餐桌预定轮播)
     * @return code:错误码
     * @return message:错误提示
     * @return data:图片信息@
     * @data id:图片ID title:图片英文名 title_zn:图片中文名 src:图片地址 type:图片类型(同参数) rid:图片关联ID created_at:图片添加时间
     */
    public function carouselList(Request $request){
        $data = $request->all();
        $limit = $data['limit'];
        $page = $data['page'];
        $type = isset($data['type']) ? $data['type'] : 3;
        $where = [
            ['type','=',$type]
        ];
        $whereDates = [];
        $return = (new Image())->getPageList(['limit'=>$limit,'page'=>$page],$where,$whereDates);
        return response()->json($return);
    }

    /**
     * @title 添加轮播图
     * @description 添加轮播图
     * @url /admin/img/lunbocre
     * @method POST
     * @module 轮播图板块
     * @author chenke
     * @param name:src type:string require:1 default: other: desc:图片地址
     * @param name:title type:string require:1 default: other: desc:图片英文名
     * @param name:title_zn type:string require:1 default: other: desc:图片中文名
     * @param name:type type:int require:1 default:0 other: desc:图片类型(3首页轮播,5餐桌预定轮播)
     * @return code:错误码
     * @return message:错误提示
     * @return data:图片ID
     */
    public function carouselAdd(Request $request){
        $data = $request->post();
        $rules = [
            'title'=>'required|string',
            'title_zn'=>'required|string',
            'src'=>'required|string',
            'type'=>[
                'required',Rule::in([3,5])
            ]
        ];
        $messages = [
            'title.required'=> '请填写英文标题',
            'title_zn.required'=> '请填写中文标题',
            'src.required'=> '请上传图片',
            'type.required'=>'请确定图片类型',
            'type.in'=>'图片类型错误'
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $result =  (new Image())->oneInsert($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'prodUpdate');
        }
        return response()->json($result);
    }
}
